import { getUserDetail, getUserProfile } from '@/api/user'
import { setToken, getToken } from '@/utils/auth'
import { login } from '@/api/user'
import { Message } from 'element-ui'
export default {
  namespaced: true,
  state: {
    token: getToken(),
    userInfo: {}
  },
  mutations: {
    updateToken(state, token) {
      state.token = token
      setToken(token)
    },
    updateUserInfo(state, info) {
      state.userInfo = info
    }
  },
  actions: {
    async getUserInfo(context) {
      try {
        const res = await getUserProfile()
        // console.log(res)
        const info = await getUserDetail(res.data.userId)
        context.commit('updateUserInfo', { ...res.data, ...info.data })
        return res.data.roles.menus
      } catch (error) {
        console.log(error)
      }
    },
    async initToken(context, loginForm) {
      const res = await login(loginForm)
      if (res.code !== 10000) return Message.error(res.message)
      Message.success(res.message)
      context.commit('updateToken', res.data)
    }

  }
}

